#include "..\..\std_lib_facilities.h"

void make_sort_file(string in_fname, string out_fname);

void read_sort_files(vector<string> fnames, string out_fname);



int main()
{
	system("chcp > 1251");
	string out_fname = "output.txt";
	vector<string> fnames;
	fnames.push_back("in.txt");
	fnames.push_back("in2.txt");
	read_sort_files(fnames, out_fname);
	
	system("pause");
	return 0;
}

void make_sort_file(string in_fname, string out_fname)
{
	ifstream ifs(in_fname.c_str());
	ofstream ofs(out_fname.c_str());
	if (!ifs) error ("Cannot read file ", in_fname);
	if (!ifs) error ("Cannot write file ", out_fname);

	string word  = "";
	vector<string> words;

	while (ifs >> word)
		if (find(words.begin(), words.end(), word)==words.end())
			words.push_back(word);
	sort(words.begin(), words.end());
	for(int i = 0; i < words.size(); ++i)
		ofs << words[i] << ' ';
}


void read_sort_files(vector<string> fnames, string out_fname)
{
	vector<string> words;
	
	for (int i = 0; i < fnames.size(); ++i)
	{
		ifstream ifs(fnames[i].c_str());
		if (!ifs) error ("Error open file ", fnames[i]);
		string word;
		while (ifs >> word)
			if (find(words.begin(), words.end(), word)==words.end())
				words.push_back(word);
	}
	ofstream ofs(out_fname.c_str());

	sort(words.begin(), words.end());
	for(int i = 0; i < words.size(); ++i)
		ofs << words[i] << ' ';
}
